<?php
/**
 * This file is lil-wide view file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * LilAppView view class
 *
 * @package       lil
 * @subpackage    lil
 */
class LilAppView extends View {
/**
 * System path to themed element: themed . DS . theme . DS . elements . DS
 *
 * @var string
 */
	var $themeElement = null;
/**
 * System path to themed layout: themed . DS . theme . DS . layouts . DS
 *
 * @var string
 */
	var $themeLayout = null;
/**
 * System path to themed: themed . DS . theme . DS
 *
 * @var string
 */
	var $themePath = null;
/**
 * Controller
 *
 * @var controller
 */
	var $pluginName = null;
/**
 * Enter description here...
 *
 * @param unknown_type $controller
 */
	function __construct(&$controller, $register = true) {
		App::import('Libs', 'Lil.LilPluginRegistry');
		
		$this->callPluginHandlers('before_construct_view');
		parent::__construct($controller, $register);
		
		$this->theme =& $controller->theme;
		$this->pluginName =& $controller->params['plugin'];
		
		$this->callPluginHandlers('after_construct_view');
	}

/**
 * Return all possible paths to find view files in order
 *
 * @param string $plugin
 * @return array paths
 * @access private
 */
	function _paths($plugin = null, $cached = true) {
		$paths = parent::_paths($plugin, $cached);
		
		// this is the case when lil plugin extends lil_controller
		if ($this->pluginName != 'lil') {
			$paths = array_merge($paths, array(dirname(__FILE__) . DS . 'views' . DS));
		}

		if (!empty($this->theme)) {
			$paths = array_merge(
				array(
					APP . 'views'. DS . 'plugins' . DS . $this->pluginName . DS . 'theme' . DS . $this->theme . DS,
					WWW_ROOT . 'theme' . DS . $this->theme . DS
				),
				$paths
			);
		}
		
		if (empty($this->__paths)) {
			$this->__paths = $paths;
		}
		
		$paths = $this->callPluginHandlers('view_paths', $paths);
		return $paths;
	}
/**
 * callPluginHandlers method
 *
 * @param string $handler
 * @access public
 * @return void
 */
	function callPluginHandlers($handler, $args = null) {
		// execute plugin handlers
		$ret = $args;
		
		$plugins = LilPluginRegistry::keys();
		foreach ((array)$plugins as $plugin_key) {
			$plugin = LilPluginRegistry::getObject($plugin_key);
			
			if (!empty($plugin->handlers[$handler]) &&
			method_exists($plugin, $plugin->handlers[$handler]['function']))
			{
				$ret = call_user_func_array(
					array($plugin, $plugin->handlers[$handler]['function']),
					array(
						'view'   => $this,
						'args'   => (array)$ret,
						'params' => (array)$plugin->handlers[$handler]['params'],
					)
				);
			}
		}
		return $ret;
	}
/**
 * getAdminAreaId method
 *
 * This functions returns current area id in administration.
 *
 * @access public
 * @return int
 */
	function getAdminAreaId() {
		if ($data = $this->getAdminArea()) {
			return $data['Area']['id'];
		} else return null;
	}
/**
 * getAdminAreaSlug method
 *
 * This functions returns current area slug in administration.
 *
 * @access public
 * @return int
 */
	function getAdminAreaSlug() {
		if ($data = $this->getAdminArea()) {
			return $data['Area'][Configure::read('Lil.areaSlugField')];
		} else return null;
	}
/**
 * getAdminArea method
 *
 * This functions returns current area in administration.
 *
 * @access public
 * @return array
 */
	function getAdminArea() {
		if (!empty($this->Session) && ($a = $this->Session->read('admin.area'))) {
			return $a;
		} else {
			return null;
		}
	}
/**
 * user method
 *
 * Returns current user
 *
 * @param mixed $key
 * @access public
 * @return mixed
 */
	function user($key = null) {
		static $session = null;
		if (empty($session)) $session = new CakeSession();
		
		if ($session->valid(Configure::read('Lil.authSessionKey'))) {
			if ($key == null) {
				return array(
					Configure::read('Lil.authModel') => 
						$session->read(Configure::read('Lil.authSessionKey'))
				);
			} else {
				$user = $session->read(Configure::read('Lil.authSessionKey'));
				if (isset($user[$key])) return $user[$key];
			}
		}
		return null;
	}
/**
 * isAdmin method
 *
 * Returns user is an administrator. 
 * If there is no user specified, current user admin status is returned.
 *
 * @param mixed $user_id
 * @access public
 * @return mixed
 */
	function isAdmin($user_id = null) {
		if (empty($user_id) || ($user_id == $this->user('id'))) {
			static $session = null;
			if (empty($session)) $session = new CakeSession();
			
			if ($session->valid(Configure::read('Lil.authSessionKey')) &&
				$user = $session->read(Configure::read('Lil.authSessionKey')))
			{
				return (bool)$user[Configure::read('Lil.userAdminField')];
			}
			return false;
		} else {
			$User =& ClassRegistry::init('Lil.User');
			return (bool)$User->field(
				Configure::read('Lil.userAdminField'),
				array('User.id' => $user_id)
			);
		}
	}
}
?>